package cn.cttic.app.service.impl;

import cn.cttic.base.api.domain.BaseCompany;
import cn.cttic.base.api.domain.bo.BaseCompanyBo;
import cn.cttic.base.api.domain.vo.BaseCompanyVo;
import cn.hutool.core.bean.BeanUtil;
import cn.cttic.common.core.utils.StringUtils;
import cn.cttic.common.mybatis.core.page.PageQuery;
import cn.cttic.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import cn.cttic.app.mapper.BaseCompanyMapper;
import cn.cttic.app.service.IBaseCompanyService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-06
 */
@RequiredArgsConstructor
@Service
public class BaseCompanyServiceImpl implements IBaseCompanyService {

    private final BaseCompanyMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public BaseCompanyVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<BaseCompanyVo> queryPageList(BaseCompanyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseCompany> lqw = buildQueryWrapper(bo);
        Page<BaseCompanyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<BaseCompanyVo> queryList(BaseCompanyBo bo) {
        LambdaQueryWrapper<BaseCompany> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseCompany> buildQueryWrapper(BaseCompanyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseCompany> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCompanyName()), BaseCompany::getCompanyName, bo.getCompanyName());
        lqw.like(StringUtils.isNotBlank(bo.getShortName()), BaseCompany::getShortName, bo.getShortName());
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyType()), BaseCompany::getCompanyType, bo.getCompanyType());
        lqw.eq(StringUtils.isNotBlank(bo.getIndustryType()), BaseCompany::getIndustryType, bo.getIndustryType());
        lqw.eq(StringUtils.isNotBlank(bo.getCreditCode()), BaseCompany::getCreditCode, bo.getCreditCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDistrict()), BaseCompany::getDistrict, bo.getDistrict());
        lqw.eq(bo.getRegisterCapital() != null, BaseCompany::getRegisterCapital, bo.getRegisterCapital());
        lqw.eq(bo.getEstablishDate() != null, BaseCompany::getEstablishDate, bo.getEstablishDate());
        lqw.eq(bo.getStartTerm() != null, BaseCompany::getStartTerm, bo.getStartTerm());
        lqw.eq(bo.getEndTerm() != null, BaseCompany::getEndTerm, bo.getEndTerm());
        lqw.eq(StringUtils.isNotBlank(bo.getRegistrationAuthority()), BaseCompany::getRegistrationAuthority, bo.getRegistrationAuthority());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), BaseCompany::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessScope()), BaseCompany::getBusinessScope, bo.getBusinessScope());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessExpertise()), BaseCompany::getBusinessExpertise, bo.getBusinessExpertise());
        lqw.eq(StringUtils.isNotBlank(bo.getIntroduction()), BaseCompany::getIntroduction, bo.getIntroduction());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessLicense()), BaseCompany::getBusinessLicense, bo.getBusinessLicense());
        lqw.eq(StringUtils.isNotBlank(bo.getLogo()), BaseCompany::getLogo, bo.getLogo());
        lqw.eq(bo.getStatus() != null, BaseCompany::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getSysUserId()), BaseCompany::getSysUserId, bo.getSysUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getAppUserId()), BaseCompany::getAppUserId, bo.getAppUserId());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(BaseCompanyBo bo) {
        BaseCompany add = BeanUtil.toBean(bo, BaseCompany.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(BaseCompanyBo bo) {
        BaseCompany update = BeanUtil.toBean(bo, BaseCompany.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseCompany entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
